// pages/goods_detail/goods_detail.js
// 导入获取数据的方法
import { request } from '../../request/index.js'
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsObj: {},
        isCollect: false
    },
    // 获取到的详情
    GoodsInfo: {},
    /**
     * 生命周期函数--监听页面加载
     */
    onShow: function () {
        let pages = getCurrentPages();
        let currentPages = pages[pages.length - 1]
        let options = currentPages.options

        const { goods_id } = options
        this.getGoodsDetail(goods_id)
    },
    //获取商品详情数据
    async getGoodsDetail(goods_id) {
        const goodsObj = await request({ url: '/goods/detail', data: { goods_id } })
        this.GoodsInfo = goodsObj
        // 获取缓存中收藏的商品数组
        let collect = wx.getStorageSync('collect') || []
        // 判断缓存中是否存在该商品数组
        let isCollect = collect.some(v => v.goods_id === this.GoodsInfo.goods_id)

        this.setData({
            goodsObj: {
                goods_name: goodsObj.goods_name,
                goods_price: goodsObj.goods_price,
                goods_introduce: goodsObj.goods_introduce.replace(/\.webp/g, '.jpg'),
                pics: goodsObj.pics
            },
            isCollect
        })
    },

    // 点击图片预览
    handlePreviewImage(e) {
        const urls = this.GoodsInfo.pics.map(v => v.pics_sma)
        const current = e.currentTarget.dataset.url
        wx.previewImage({
            urls,
            current
        })
    },
    // 加入购物车
    handleCartAdd() {
        // 获取缓存中的数据
        let cart = wx.getStorageSync("cart") || []
        // 判断商品是否已经存在购物车
        let index = cart.findIndex(v => v.goods_id === this.GoodsInfo.goods_id)
        if (index === -1) {
            // 不存在，加入到购物车
            this.GoodsInfo.num = 1
            this.GoodsInfo.checked = true
            cart.push(this.GoodsInfo)
        } else {
            // 存在,数量++
            cart[index].num++
        }
        // 把购物车重新添加到缓存中
        wx.setStorageSync('cart', cart)
        // 弹窗提示
        wx.showToast({
            title: '加入成功',
            icon: 'success',
            mask: true//显示透明蒙层，防止触摸穿透(防抖)
        })
    },
    handleCollect() {
        let isCollect = false
        // 获取缓存中收藏的商品
        let collect = wx.getStorageSync('collect') || []
        // 判断是否有该商品
        let index = collect.findIndex(v => v.goods_id === this.GoodsInfo.goods_id)
        // 当index不等于-1 ，说明已经收藏过该商品，则取消收藏 否则，收藏该商品
        if (index !== -1) {
            collect.splice(index, 1)
            isCollect = false
            wx.showToast({
                title: '取消收藏',
                icon: "success",
                mask: true
            })
        } else {
            collect.push(this.GoodsInfo)
            isCollect = true
            wx.showToast({
                title: '收藏成功',
                icon: "success",
                mask: true

            })
        }
        // 把该商品重新存到缓存中
        wx.setStorageSync('collect', collect)
        this.setData({
            isCollect
        })
    }
})